﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using PSTaskDialog;

namespace ArduinoSketch.Core
{
    public partial class HardwareTarget : UserControl
    {
        private Hardwares _hardware;

        public HardwareTarget()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            _hardware = Utils.LoadHardwardBoard();
            this.LoadHardwareProfile();
        }

        private void LoadHardwareProfile()
        {
            this.listView.Items.Clear();
            int _idx = 1;
            foreach (var profile in _hardware.Profile)
            {
                ListViewItem _item = new ListViewItem(_idx.ToString());
                _item.Tag = profile;
                _item.SubItems.Add(profile.Name);

                _idx++;
                this.listView.Items.Add(_item);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count == 1)
            {
                ListViewItem _itemSelected = listView.SelectedItems[0];
                HardwareProfile _profileSelected = (HardwareProfile)_itemSelected.Tag;
                if (HardwareTargetDialog.ShowUpdateDialog(_profileSelected) == DialogResult.OK)
                {
                    XmlSerializable.WriteFile<Hardwares>(_hardware, StringConst.BOARD_CNFG);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count == 1)
            {
                ListViewItem _itemSelected = listView.SelectedItems[0];
                HardwareProfile _profileSelected = (HardwareProfile)_itemSelected.Tag;
                DialogResult _dialogResult = PSTaskDialog.cTaskDialog.MessageBox(
                            "Question",
                            "Delete profile",
                            String.Format("Are you sure you want to delete \"{0}\" nform profile", _profileSelected.Name),
                            eTaskDialogButtons.OKCancel,
                            eSysIcons.Question);

                if (_dialogResult == DialogResult.OK)
                {
                    _hardware.Profile.Remove(_profileSelected);
                    XmlSerializable.WriteFile<Hardwares>(_hardware, StringConst.BOARD_CNFG);
                    this.LoadHardwareProfile();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (HardwareTargetDialog.ShowNewDialog() == DialogResult.OK)
            {
                HardwareProfile _newProfile = HardwareTargetDialog.GetHardwareProfile();
                _hardware.Profile.Add(_newProfile);
                XmlSerializable.WriteFile<Hardwares>(_hardware, StringConst.BOARD_CNFG);
                this.LoadHardwareProfile();
            }
        }
    }
}
